package com.hk.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gc.materialdesign.views.ButtonFlat;
import com.gc.materialdesign.widgets.SnackBar;
import com.hk.R;
import com.hk.biz.BLL;
import com.hk.model.Question;
import com.hk.util.Utils;
import com.umeng.analytics.MobclickAgent;

public class Question3Activity extends Activity {

	private TextView mtvTitle;
	private TextView mtvChooseA;
	private TextView mtvChooseB;
	private TextView mtvChooseC;
	private TextView mtvChooseD;
	private RelativeLayout mlrA;
	private RelativeLayout mlrB;
	private RelativeLayout mlrC;
	private RelativeLayout mlrD;
	private Button mbtnChangeColor;
	private ViewPager viewPager;
	private TextView mtvNowQuestion;
	private TextView mtvSumQuestion;
	private LinearLayout mrlAnswer;
	private TextView mtvAnswer;
	private TextView mtvAnalysis;
	private Button mbtn_back;
	private TextView mtvPTitle;
	private TextView mtvhead;

	private String mPid;
	private String mQType, mHead;
	private boolean useTheme = true;
	private Bundle savedInstanceState;
	private List<View> viewList;
	private List<Question> listQuestion;
	private int pos = 0;// 当前题目
	private int all = 3;// 总题目数
	private String mPageName = "Question3Activity";
	private String pTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (useTheme) {
			setTheme(R.style.ThemeDay);
		} else {
			setTheme(R.style.ThemeNight);
		}

		setContentView(R.layout.activity_question3);
		this.savedInstanceState = savedInstanceState;
		findByID();

		Bundle bundle = this.getIntent().getExtras();
		mPid = bundle.getString("Pid");
		pTitle = bundle.getString("PTitle");
		mQType = bundle.getString("QuestionType");
		mHead = bundle.getString("Head");

		listQuestion = BLL.getQuestionListByPid(getApplicationContext(), mPid);
		all = listQuestion.size();

		viewList = new ArrayList<View>();
		LayoutInflater mLi = LayoutInflater.from(this);
		for (int i = 0; i < all; i++) {
			View v = mLi.inflate(R.layout.viewpage_question, null);
			viewList.add(v);
		}

		setIndicate();

		viewPager.setAdapter(new MyAdapter());
		viewPager.setOnPageChangeListener(new MyPagerChangeListener());
		// viewPager.setCurrentItem(pos);

		mbtnChangeColor.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				changeColor();
			}
		});
		mbtn_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {

				new SnackBar(Question3Activity.this, "确定要退出学习吗？", "是的",
						new OnClickListener() {

							@Override
							public void onClick(View v) {
								finish();
							}
						}).show();
			}
		});
		setBtnBg();

		mtvPTitle.setText(pTitle);
		mtvhead.setText(mHead);
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(mPageName);
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(mPageName);
		MobclickAgent.onPause(this);
	}

	@Override
	public void finish() {
		super.finish();
	}
	
	public void setBtnBg(){
		if(useTheme){
			mbtnChangeColor.setBackgroundResource(R.drawable.btn_night);
		}else{
			mbtnChangeColor.setBackgroundResource(R.drawable.btn_sun);
		}
	}

	// 查询ID
	protected void findByID() {
		viewPager = (ViewPager) findViewById(R.id.vpQuestion);
		mbtnChangeColor = (Button) this.findViewById(R.id.btnChangeColor);
		mtvNowQuestion = (TextView) this.findViewById(R.id.tvNowQuestion);
		mtvSumQuestion = (TextView) this.findViewById(R.id.tvSumQuestion);
		mbtn_back = (Button) this.findViewById(R.id.btn_back);
		mtvPTitle = (TextView) this.findViewById(R.id.tvPTitle);
		mtvhead = (TextView) this.findViewById(R.id.tvhead);
	}

	// 修改主题
	public void changeColor() {
		useTheme = !useTheme;
		onCreate(Question3Activity.this.savedInstanceState);
	}

	// 设置当前题目数和总题目数
	public void setIndicate() {
		mtvNowQuestion.setText(String.valueOf(pos + 1));
		mtvSumQuestion.setText(String.valueOf(all));
	}

	// ViewPage的数据项
	public class MyAdapter extends PagerAdapter {

		MyAdapter() {

		}

		@Override
		public int getCount() {
			return all;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {

			View v = viewList.get(position);
			mtvTitle = (TextView) v.findViewById(R.id.tvTitle);

			mtvChooseA = (TextView) v.findViewById(R.id.tvChooseA);
			mtvChooseB = (TextView) v.findViewById(R.id.tvChooseB);
			mtvChooseC = (TextView) v.findViewById(R.id.tvChooseC);
			mtvChooseD = (TextView) v.findViewById(R.id.tvChooseD);

			mlrA = (RelativeLayout) v.findViewById(R.id.lrA);
			mlrB = (RelativeLayout) v.findViewById(R.id.lrB);
			mlrC = (RelativeLayout) v.findViewById(R.id.lrC);
			mlrD = (RelativeLayout) v.findViewById(R.id.lrD);

			Question q = listQuestion.get(position);
			mtvTitle.setText(q.Title);
			mtvChooseA.setText(q.Answer1);
			mtvChooseB.setText(q.Answer2);
			mtvChooseC.setText(q.Answer3);
			mtvChooseD.setText(q.Answer4);

			if (q.Answer1 == null || q.Answer1.equals("")) {
				mlrA.setVisibility(View.GONE);
			}
			if (q.Answer2 == null || q.Answer2.equals("")) {
				mlrB.setVisibility(View.GONE);
			}
			if (q.Answer3 == null || q.Answer3.equals("")) {
				mlrC.setVisibility(View.GONE);
			}
			if (q.Answer4 == null || q.Answer4.equals("")) {
				mlrD.setVisibility(View.GONE);
			}

			mlrA.setOnClickListener(new ChooseClickedListener());
			mlrB.setOnClickListener(new ChooseClickedListener());
			mlrC.setOnClickListener(new ChooseClickedListener());
			mlrD.setOnClickListener(new ChooseClickedListener());

			container.addView(v);
			return v;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(viewList.get(position));
		}
	}

	// ViewPage的页面监听
	class MyPagerChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onPageSelected(int position) {
			pos = position;
			setIndicate();
		}
	}

	// 答案选择监听
	class ChooseClickedListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			View view = viewList.get(pos);

			Question question = listQuestion.get(pos);
			String rightchecked = question.Answer;

			RelativeLayout rlChoose = (RelativeLayout) view.findViewById(v
					.getId());
			String choosed = String.valueOf(rlChoose.getTag());
			String tag = "btn_" + choosed;
			Button btnChoose = (Button) rlChoose.findViewWithTag(tag);
			btnChoose.setText("");
			if (choosed.equals(rightchecked)) {
				btnChoose.setBackgroundResource(R.drawable.choose_y);
			} else {
				btnChoose.setBackgroundResource(R.drawable.choose_n);

				RelativeLayout rlRightChoose = (RelativeLayout) view
						.findViewWithTag(rightchecked);
				Button btnRightChoose = (Button) rlRightChoose
						.findViewWithTag("btn_" + rightchecked);
				btnRightChoose.setText("");
				btnRightChoose.setBackgroundResource(R.drawable.choose_y);
			}

			mrlAnswer = (LinearLayout) view.findViewById(R.id.rlAnswer);
			mtvAnswer = (TextView) view.findViewById(R.id.tvAnswer);
			mtvAnalysis = (TextView) view.findViewById(R.id.tvAnalysis);
			mtvAnswer.setText(rightchecked);
			mtvAnalysis.setText(question.Analysis);
			mrlAnswer.setVisibility(View.VISIBLE);

		}
	}

}
